package com.fourth.fronted.service.impl;

import com.fourth.entity.Address;
import com.fourth.fronted.dao.AddressMapper;
import com.fourth.fronted.service.AddressService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;

    @Override
    public List<Address> queryById(Integer uid) {
        return addressMapper.queryById(uid);
    }

    /**
     * 根据uid 先重置所有收货地址的状态为0，然后修改选中的收货地址为1
     * @param uId 用户ID
     * @param aId 地址ID
     * @return
     */

    @Override
    public int changeTakeDeliveryAddress(Integer uId, Integer aId) {

        System.out.println("aId:" + aId + "uId:" + uId);

        //重置收获地址的状态都为 0
        int i = addressMapper.resetTakeDeliveryAddress(uId);

        System.out.println("收货地址重置状态" + i);

        //修改收获地址的状态为  1
        int i1 = addressMapper.changeTakeDeliveryAddress(aId);

        System.out.println("收货地址修改状态" + i1);

        if (i == i1) {
            return 1;
        }
        return 0;
    }

    /**
     * 根据uid获取所有的地址,然后地址的默认状态全改为0，最后把选中的地址状态改为1
     * @param uId
     * @param aId
     * @return
     */
    @Override
    public int defaultAddress(Integer uId, Integer aId) {

        //重置默认地址的状态都为 0
        int i = addressMapper.resetDefaultAddress(uId);
        System.out.println("默认地址重置: " + i);
        //修改默认地址的状态为  1
        int i1 = addressMapper.changeDefaultAddress(aId);
        System.out.println("默认地址修改: " + i1);
        if(i == i1){
            return 1;
        }
        return 0;
    }

    @Override
    public Address selectTDAddressByUid(Integer uId) {
        return addressMapper.selectTDAddressByUid(uId);
    }

    @Override
    public int insertAddress(Address address,@Param("uId") Integer uId) {
        return addressMapper.insertAddress(address,uId);
    }


}
